package www.wxwnb.clerk.ui.activity;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.wxwnb.clerk.R;
import www.wxwnb.clerk.adapter.MsgQueryAdapter;
import www.wxwnb.clerk.base.BaseActivity;
import www.wxwnb.clerk.decoration.Divider12ItemDecoration;
import www.wxwnb.clerk.http.HttpMethods;
import www.wxwnb.clerk.http.HttpService;
import www.wxwnb.clerk.http.entity.MsgQueryBean;
import www.wxwnb.clerk.utils.Customer;

public class ReviewActivity extends BaseActivity {

    private static final String TAG = "ReviewActivity";
    private TextView title;
    private ImageButton ibtnBack;
    private RecyclerView mRecyclerView;
    private int index = 1;
    private MsgQueryAdapter mMsgQueryAdapter;
    private SpringView mSpringView;
    private Context mContext;
    private List<MsgQueryBean.DataBean.PageRecordsBean> dataList = new ArrayList();
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);
    }

    @Override
    protected void initView() {
        mContext = this;
        ibtnBack = (ImageButton) findViewById(R.id.ibtn_back);
        title = (TextView) findViewById(R.id.app_title);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_review);
        mSpringView = (SpringView) findViewById(R.id.springView);
    }

    @Override
    public void initListener() {
        ibtnBack.setOnClickListener(this);
        mSpringView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                dataList.clear();
                index = 1;
                getDataFromServer(index);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSpringView.onFinishFreshAndLoad();
                    }
                }, 1000);
            }

            @Override
            public void onLoadmore() {
                //加载更多
                index = index + 1;
                getDataFromServer(index);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSpringView.onFinishFreshAndLoad();
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void initData() {
        title.setText(R.string.review_notice);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.addItemDecoration(new Divider12ItemDecoration());

        getDataFromServer(index);
        mSpringView.setHeader(new DefaultHeader(mContext));
        mSpringView.setFooter(new DefaultHeader(mContext));
    }

    private void getDataFromServer(int index) {
        HttpService httpService = HttpMethods.getInstance().mRetrofit.create(HttpService.class);
        Call<MsgQueryBean> msgQuery = httpService.getMsgQuery(Customer.getInstance().getJobNumber(), String.valueOf(index));
        msgQuery.enqueue(new Callback<MsgQueryBean>() {
            @Override
            public void onResponse(Call<MsgQueryBean> call, Response<MsgQueryBean> response) {
                if (response.isSuccessful()) {
                    if (response.body().getReturnCode() == 1) {
                        handlerResponse(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<MsgQueryBean> call, Throwable t) {
            }
        });
    }

    private void handlerResponse(MsgQueryBean.DataBean data) {
        if (data.getPageNum() < data.getPageSize()) {   //当前页码 总页码
            mSpringView.getHeaderView().setVisibility(View.VISIBLE);
            mSpringView.getFooterView().setVisibility(View.VISIBLE);
        } else if (data.getPageNum() == data.getPageSize()) {
            mSpringView.getHeaderView().setVisibility(View.VISIBLE);
            mSpringView.getFooterView().setVisibility(View.GONE);
        }

        dataList.addAll(data.getPageRecords());

        if (mMsgQueryAdapter == null) {
            mMsgQueryAdapter = new MsgQueryAdapter(this, dataList);
            mRecyclerView.setAdapter(mMsgQueryAdapter);
        } else {
            mMsgQueryAdapter.setData(dataList);
        }
    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
        }
    }
}
